package com.zmx.common.common.utils;

import com.zmx.common.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;

import java.io.UnsupportedEncodingException;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 通用基础工具类
 * @ClassName: Utils
 * @Author zhaomxr
 * @Date 2021-10-12 16:32
 */
@Slf4j
public class Utils {

    public static String getIp() {
        try {
            return Inet4Address.getLocalHost().getHostAddress();
        } catch (UnknownHostException e) {
            log.error("获取本机ip地址失败", e);
            return "";
        }
    }

    public static String base64EnCode(String code) {
        String enCode = "";
        try {
            Base64.Encoder encoder = Base64.getEncoder();
            byte[] codeByte = new byte[0];
            codeByte = code.getBytes("UTF-8");
            enCode = encoder.encodeToString(codeByte);
            System.out.println(enCode);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
            throw new BusinessException("Base64 编码码失败！", e);
        }
        return enCode;
    }

    public static String base64DeCode(String code) {
        String deCode = "";
        try {
            Base64.Decoder decoder = Base64.getDecoder();
            final byte[] codeByte = code.getBytes("UTF-8");
            deCode = new String(decoder.decode(codeByte), "UTF-8");
            System.out.println(deCode);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(), e);
            throw new BusinessException("Base64 解码失败！", e);
        }
        return deCode;
    }

    /**-------------------------驼峰与下划线相互转换----------------------------**/
    private static Pattern linePattern = Pattern.compile("_(\\w)");

    /**
     * 下划线转驼峰
     * @param str
     * @return
     */
    public static String lineToHump(String str) {
        str = str.toLowerCase();
        Matcher matcher = linePattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    private static Pattern humpPattern = Pattern.compile("[A-Z]");
    /**
     * 驼峰转下划线
     * @param str
     * @return
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    /**
     * 驼峰转下划线
     * @param str
     * @return
     */
    public static Map<String, Object> humpToLine(Map<String, Object> data) {
        Map<String, Object> result = new HashMap<String, Object>(data.size());
        if (data != null && data.size() > 0) {
            for (String key : data.keySet()) {
                result.put(humpToLine(key), data.get(key));
            }
        }
        return result;
    }
    /**-------------------------驼峰与下划线相互转换----------------------------**/


    private static double EARTH_RADIUS = 6371.393;
    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }
    /**
     * 计算两个经纬度之间的距离
     * @return
     */
    public static double GetDistance(double lat1, double lng1, double lat2, double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.abs(Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 1000);
        return s;
    }
}
